----------------------------------------
-- 好友系统DB
-- addFriendData 		添加好友
-- delFriendData		删除好友
-- getFriendUuids		获得好友列表
----------------------------------------

local LuaMongo = _G.lua_mongo
local DB = require("common.DB")
local FriendDefine = require("friend.FriendDefine")
local RoleDBLogic = require("role.RoleDBLogic")

local DBOpAnd = {uuid1 = nil, uuid2 = nil}
local DBOpOr = {["$or"] = {{uuid1=""}, {uuid2=""}}}	
local DBOpRemove = {_id = nil}	
local DBOpHeartRemove1 = {uuid1=nil, isGetRedHeart1=nil}
local DBOpHeartRemove2 = {uuid2=nil, isGetRedHeart2=nil}
		
function isFriend(uuid1, uuid2)
	if uuid1 > uuid2 then
		uuid1, uuid2 = uuid2, uuid1
	end
	DBOpAnd.uuid1 = uuid1
	DBOpAnd.uuid2 = uuid2
	local data = {}
	LuaMongo.find(DB.db_friend, DBOpAnd)
	if not LuaMongo.next(data) then
		return 
	end
	return true, data
end

function addFriendData(uuid1, uuid2)
	if isFriend(uuid1, uuid2) then
		return
	end
	if uuid1 > uuid2 then
		uuid1, uuid2 = uuid2, uuid1
	end	
	local data = {}
	data.uuid1 = uuid1
	data.uuid2 = uuid2
    data.isGetRedHeart1 = nil -- uuid2是否发了爱心给uuid1的状态 1已发未领取 2已发已领取 
    data.isGetRedHeart2 = nil -- uuid1是否发了爱心给uuid2的状态 1已发未领取 2已发已领取
	LuaMongo.insert(DB.db_friend, data)
	return true, data
end

function delFriendData(uuid1, uuid2)
	local pass, data = isFriend(uuid1, uuid2)
	if not pass then return end

	DBOpRemove._id = data._id
	LuaMongo.remove(DB.db_friend, DBOpRemove)
	return true
end


function getFriendUuids(uuid)
    local friendUuidList = {}
	DBOpOr["$or"][1].uuid1 = uuid
	DBOpOr["$or"][2].uuid2 = uuid
	LuaMongo.find(DB.db_friend, DBOpOr)
	local cnt = 0
	while true do
		local data = {}
		if not LuaMongo.next(DB.db_friend, data) then
			break
		end
		cnt = cnt + 1
		if data.uuid1 ~= uuid then
            if friendUuidList[cnt] == nil then
               friendUuidList[cnt] = {}
            end
			friendUuidList[cnt].uuid = data.uuid1
            friendUuidList[cnt].getStatus = data.isGetRedHeart2 --如果玩家是1号位置 我要看1号位置给2号位置 的赠送信息 那就是2
		elseif data.uuid2 ~= uuid then
            if friendUuidList[cnt] == nil then
               friendUuidList[cnt] = {}
            end
			friendUuidList[cnt].uuid = data.uuid2
            friendUuidList[cnt].getStatus = data.isGetRedHeart1 --如果玩家是2号位置 我要看2号位置给1号位置 的赠送信息 那就是1
		else
			assert()
		end
	end
	return cnt, friendUuidList
end

-- 设置红心领取状态
function setFriendHeart(uuid1, uuid2, status, isgive)
    local tempTb = {}
    local tb = {}
    if uuid1 > uuid2 then
        uuid1, uuid2 = uuid2, uuid1
        if isgive then
           tb.isGetRedHeart1 = status
        else
           tb.isGetRedHeart2 = status
        end
        tempTb["$set"] = tb
	else
        if isgive then
           tb.isGetRedHeart2 = status
        else
           tb.isGetRedHeart1 = status
        end

        tempTb["$set"] = tb
    end	

	DBOpAnd.uuid1 = uuid1
    DBOpAnd.uuid2 = uuid2

	LuaMongo.update(DB.db_friend, DBOpAnd, tempTb)
end

-- 清空红心状态
function delAllFriendHeart(uuid)
    
    --== uuid1的数据 清空isGetRetHeart1
    local tempTb1 = {}
    DBOpHeartRemove1.uuid1 = uuid
    DBOpHeartRemove1.isGetRedHeart1=2
    local updatedata1 = {}
    updatedata1.isGetRedHeart1=1
    tempTb1["$unset"] = updatedata1
	LuaMongo.update(DB.db_friend, DBOpHeartRemove1, tempTb1)

    --== uuid2的数据 清空isGetRetHeart2
    local tempTb2 = {}
    DBOpHeartRemove2.uuid2 = uuid
    DBOpHeartRemove2.isGetRedHeart2=2
    local updatedata2 = {}
    updatedata2.isGetRedHeart2=1
    tempTb2["$unset"]=updatedata2
	LuaMongo.update(DB.db_friend, DBOpHeartRemove2, tempTb2)
end

-------------------------部分东西只存内存--------------------------------
local requestList = {}

-- uuid1向uuid2申请
function hasRequest(uuid1, uuid2)
	if not requestList[uuid2] then
		return
	end
	if not requestList[uuid2][uuid1] then
		return
	end
	return true
end

-- uuid1向uuid2申请
function addRequest(uuid1, uuid2)
	if not requestList[uuid2] then
		requestList[uuid2] = {}
	end	
	if not requestList[uuid2][uuid1] then
		requestList[uuid2][uuid1] = os.time()
	end
end

-- 获得uuid的申请列表
function getRequests(uuid)
	return requestList[uuid]
end

-- 删除uuid1向uuid2申请
function delRequest(uuid1, uuid2)
	if not requestList[uuid2] then
		return
	end
	if not requestList[uuid2][uuid1] then
		return
	end
	requestList[uuid2][uuid1] = nil
	return true
end

function haveRequest(uuid)
   if not requestList[uuid] then
	  return 0
   end
   
    for k, v in pairs(requestList[uuid]) do
		if v ~= nil then
			return 1
		end
	end
	
    return 0
end

--uuid1是否被uuid2拉黑
function isBlackFriend(uuid1,uuid2)
	local db = RoleDBLogic.getDb(uuid2,"friendBlack")
	if not db or not db.friendBlack then return end
	
	return db.friendBlack[uuid1]
end